<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.spuId ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-form-item label="物料编号" prop="spuNo">
        <el-input :disabled="dataForm.spuId ? true : false" v-model="dataForm.spuNo" placeholder="物料编号"></el-input>
      </el-form-item>
      <el-form-item label="物料名称" prop="spuName">
        <el-input v-model="dataForm.spuName" placeholder="物料名称"></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="spuUnit">
        <el-select v-model="dataForm.spuUnit" style="width: 370px" placeholder="请选择" clearable>
          <el-option v-for="item in units" :key="item.dictTypeId" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { getDictValue } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        spuId: '',
        spuNo: '',
        spuName: '',
        equipName: '',
        equipType: '',
        spuSpec: '',
        spuUnit: ''
      },
      total: 0,
      optionsParams: {
        spuNo: '',
        page: 1,
        limit: 20000
      },
      spuOptions: [],
      equipTypes: [],
      equipNames: [],
      units: []
    }
  },
  created() {
    this.queryOptions()
    this.$nextTick(() => {
      // 获取设备类型字典
      getDictValue('spuUnit').then(({ data: res }) => {
        if (res.code !== 0) {
          this.units = []
          this.total = 0
          return this.$message.error(res.msg)
        }
        this.units = res.data
        this.total = res.data.total
      })
    })
  },
  computed: {
    dataRule() {
      return {
        spuNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.dataForm.spuId = this.dataForm.id
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.spuId) {
          this.getInfo()
        }
      })
    },
    queryOptions() {
      this.$http.get('/wms/spu/page', { params: this.optionsParams }).then(({ data: res }) => {
        if (res.code !== 0) {
          this.dataLspuOptionsist = []
          this.total = 0
          return this.$message.error(res.msg)
        }
        this.spuOptions = res.data.list
        this.total = res.data.total
      })
    },
    // 物料编号
    selectChange(e) {
      this.spuOptions.find((item) => {
        if (item.spuNo == e) {
          this.dataForm.spuName = item.spuName;
          this.dataForm.equipName = item.equipName;
          this.dataForm.equipType = item.equipType;
          this.dataForm.spuSpec = item.spuSpec;
          this.dataForm.spuUnit = item.spuUnit;
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/wms/spu/${this.dataForm.spuId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data,
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.spuId ? 'post' : 'put']('/wms/spu/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
